package com.xuqm.base;

import android.app.Application;
import android.content.Context;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import androidx.annotation.Nullable;

import com.orhanobut.logger.AndroidLogAdapter;
import com.orhanobut.logger.FormatStrategy;
import com.orhanobut.logger.Logger;
import com.orhanobut.logger.PrettyFormatStrategy;
import com.xuqm.base.di.component.AppComponent;
import com.xuqm.base.di.manager.HttpManager;

public class App extends Application {

    public AppComponent appComponent;
    // 宽高
    private int width = 0, height = 0;


    private static App instance;

    public static App getInstance() {
        if (null == instance) {
            synchronized (App.class) {
                if (null == instance)
                    instance = new App();
            }
        }
        return instance;
    }

    public App() {
        instance = this;
        handler = new Handler();
        appComponent = HttpManager.getAppComponent("");
    }

    //https://www.wanandroid.com/wxarticle/list/408/1/json
    @Override
    public void onCreate() {
        super.onCreate();
        DisplayMetrics dm = new DisplayMetrics();
        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        if (null != wm) {
            wm.getDefaultDisplay().getMetrics(dm);
            width = dm.widthPixels;// 屏幕宽度
            height = dm.heightPixels;// 屏幕高度
        }

        FormatStrategy formatStrategy = PrettyFormatStrategy.newBuilder()
                .showThreadInfo(false)  // (Optional) Whether to show thread info or not. Default true
                .methodCount(0)         // (Optional) How many method line to show. Default 2
                .methodOffset(2)        // (Optional) Hides internal method calls up to offset. Default 5
                //.logStrategy(customLog) // (Optional) Changes the log strategy to print out. Default LogCat
                .tag("LogHttpInfo")   // (Optional) Global tag for every log. Default PRETTY_LOGGER
                .build();

        Logger.addLogAdapter(new AndroidLogAdapter(formatStrategy) {
            @Override
            public boolean isLoggable(int priority, @Nullable String tag) {
                return BuildConfig.DEBUG;
            }
        });


    }

    public int getHeight() {
        return height;
    }

    public int getWidth() {
        return width;
    }

    private final Handler handler;

    /**
     * 提交主线程处理
     *
     * @param runnable runnable
     */
    public void runOnUiThread(final Runnable runnable) {
        handler.post(runnable);
    }

    /**
     * 提交主线程，延时后处理
     *
     * @param runnable runnable
     * @param delayMillis 延时时间
     */
    public void runOnUiThreadDelay(final Runnable runnable, long delayMillis) {
        handler.postDelayed(runnable, delayMillis);
    }

}
